﻿using DH.Core;
using DH.Core.MapperHelper;
using DH.Data;
using DH.Data.ORM.EF.Extensions;
using DH.Entity;
using DH.Service.ViewModels;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DH.Service.Domain
{
   public class ButtonService: BaseService<ButtonEntity, string>
    {
        protected IServiceCollection _serviceCollection;
        
        public ButtonService(IServiceCollection service, DbCoreUnitOfWork unitOfWork) : base(service,unitOfWork)
        {
            this._serviceCollection = service;
            
        }
        public async  Task<PagedData<ButtonEntity>> GetList(string key, ButtonQueryDto filter, Pagination page)
        {
            var buttonQuery =await  GetAllAsync() as IQueryable<ButtonEntity>;
            buttonQuery = buttonQuery.Where(c => c.Id != Guid.Empty.ToString());
            if (!string.IsNullOrWhiteSpace(key))
            {
                buttonQuery = buttonQuery.Where(c => (c.Name.Contains(key)));
            }
            if (filter.status != null)
            {
                buttonQuery = buttonQuery.Where(c => (c.Status == filter.status));
            }

            var tableEntityList = buttonQuery.OrderBy(c => c.CreatedOn).TakePageData(page);
            return tableEntityList;
        }

        public ButtonEntity Add(ButtonViewModel viewModel)
        {
            ButtonEntity model;
            model = DHMapper.Map<ButtonViewModel, ButtonEntity>(viewModel);
            model.CreatedOn = DateTimeOffset.Now;
            model.UpdatedOn = DateTimeOffset.Now;
            model.SortIndex = 0;

            InsertSync(model);

            return model;

        }
        
        public ButtonEntity Edit(ButtonViewModel viewModel)
        {
            var oldObj = GetSync(viewModel.Id);
            var entityObj = DHMapper.Map<ButtonViewModel, ButtonEntity>(viewModel, oldObj);

            UpdateSync(entityObj);

            return entityObj;
        }

        public void Delete(string[] ids)
        {
            var models = GetByQuery(c => ids.Contains(c.Id));
            DeleteSync(models);
        }

        public bool Enabled(string id)
        {

            var userObj = GetSync(id);
            if (userObj != null)
            {
                userObj.Status = true;
                UpdateSync(userObj);
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool Disabled(string id)
        {
            var userObj = GetSync(id);
            if (userObj != null)
            {
                userObj.Status = false;
                UpdateSync(userObj);
                return true;
            }
            else
            {
                return false;
            }

        }

        public List<ButtonEntity> GetButtons(bool? status)
        {
            if (status!=null)
            {
                var res = GetByQuery(c=>c.Status==status).ToList();
                return res;
            }
            else
            {
                var res = GetAllSync().ToList();
                return res;
            }
            
        }
        public List<ButtonRelevanceDto> GetButtonsRelevances(bool? status)
        {
            var res = new List<ButtonEntity>();
            if (status != null)
            {
                res = GetByQuery(c => c.Status == status).ToList();
                

            }
            else
            {
                res = GetAllSync().ToList();
                
            }

            var dataList = res.Select(c => {
                var dicViewModel = DHMapper.Map<ButtonEntity, ButtonRelevanceDto>(c);

                return dicViewModel;
            }).ToList();
            return dataList;
        }

        #region  异步操作
        public async Task<ButtonEntity> AddAsync(ButtonViewModel viewModel)
        {
            ButtonEntity model;
            model = DHMapper.Map<ButtonViewModel, ButtonEntity>(viewModel);
            model.CreatedOn = DateTimeOffset.Now;
            model.UpdatedOn = DateTimeOffset.Now;
            model.SortIndex = 0;

            await InsertAsync(model);

            return model;
        }
        public async Task<ButtonEntity> EditAsync(ButtonViewModel viewModel)
        {
            var oldObj = GetSync(viewModel.Id);
            var entityObj = DHMapper.Map<ButtonViewModel, ButtonEntity>(viewModel, oldObj);

            await UpdateAsync(entityObj);

            return entityObj;
        }

        public async Task DeleteAsync(string[] ids)
        {
            var models = GetByQuery(c => ids.Contains(c.Id));
            await DeleteAsync(models);
        }
        public async Task<bool> EnabledAsync(string id)
        {
            var userObj =await GetAsync(id);
            if (userObj != null)
            {
                userObj.Status = true;
                await UpdateAsync(userObj);
                return true;
            }
            else
            {
                return false;
            }
        }

        public async Task<bool> DisabledAsync(string id)
        {
            var userObj =await GetAsync(id);
            if (userObj != null)
            {
                userObj.Status = false;
                await UpdateAsync(userObj);

                return true;
            }
            else
            {
                return false;
            }
        }

        public async Task<List<ButtonEntity>> GetButtonsAsync(bool? status)
        {
            if (status != null)
            {
                var res =await GetByQueryAsync(c => c.Status == status);
                return res.ToList();
            }
            else
            {
                var res = await GetAllAsync();
                return res.ToList();
            }
        }

        public async Task<List<ButtonRelevanceDto>> GetButtonsRelevancesAsync(bool? status)
        {
            var res = new List<ButtonEntity>();
            if (status != null)
            {
                res = (await GetByQueryAsync(c => c.Status == status)).ToList();


            }
            else
            {
                res = (await GetAllAsync()).ToList();

            }

            var dataList = res.Select(c => {
                var dicViewModel = DHMapper.Map<ButtonEntity, ButtonRelevanceDto>(c);

                return dicViewModel;
            }).ToList();
            return dataList;
        }

        #endregion


    }
}
